package com.bianguo.android.iocdemo.view.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.bianguo.android.injectlibrary.utils.MLog;

public class MoveView extends View {

    private Paint mPaint;
    private RectF mRectF;
    private float mPreX, mPreY;
    private boolean include;
    private OnMoveListener onMoveListener;
    private int touchMode;

    public MoveView(Context context) {
        this(context, null);
    }

    public MoveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MoveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(10);

        mRectF = new RectF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mPreX = w / 2;
        mPreY = h / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mRectF.left = mPreX - 100;
        mRectF.right = mPreX + 100;
        mRectF.top = mPreY - 100;
        mRectF.bottom = mPreY + 100;
        canvas.drawRect(mRectF, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                touchMode = 0;
                float x = event.getX();
                float y = event.getY();
                if (x > mRectF.left && x < mRectF.right && y > mRectF.top && y < mRectF.bottom) {
                    mPreX = event.getX();
                    mPreY = event.getY();
                    include = true;
                } else {
                    include = false;
                    if (onMoveListener != null) {
                        onMoveListener.touchOutside();
                    }
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                touchMode = 1;
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:
                if (0 == touchMode) {
                    if (include) {
                        mPreX = event.getX();
                        mPreY = event.getY();
                    }
                } else if (1 == touchMode) {
                    getPointDistance(event);
                }
                break;
            case MotionEvent.ACTION_UP:
                include = false;
                break;
        }
        if (include) {
            invalidate();
        }
//        return include;
        return true;
    }

    private void getPointDistance(MotionEvent event) {
        if (event.getPointerCount() >= 2) {
            float distanceX = Math.abs(event.getX(0) - event.getX(1));
            float distanceY = Math.abs(event.getY(0) - event.getY(1));
            MLog.i("distanceX = " + distanceX + ", distanceY = " + distanceY);
        }
    }

    public void setOnMoveListener(OnMoveListener onMoveListener) {
        this.onMoveListener = onMoveListener;
    }

    public interface OnMoveListener {

        void touchOutside();

        void onMove();
    }
}
